//
//  MenuSearchViewController.m
//  项目三
//
//  Created by rimi1 on 2016/12/9.
//  Copyright © 2016年 Wangxingqian. All rights reserved.
//

#import "MenuSearchViewController.h"
#import "DetailViewController.h"

#import "MenuListTableViewCell.h"
#import "MenuListModel.h"
#import "NotView.h"

#import "NetRequest+Menu.h"

@interface MenuSearchViewController () <UITableViewDelegate, UITableViewDataSource, UIScrollViewDelegate>

@property (nonatomic, strong) UITextField *searchBar;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *modelArr;
@property (nonatomic, strong) NotView *notView;

@end

@implementation MenuSearchViewController

- (void)viewDidAppear:(BOOL)animated {
    [self.searchBar becomeFirstResponder];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initializeDataSource];
    [self initializeAppearance];
}

#pragma mark - initialize

- (void)initializeDataSource {
    self.modelArr = [NSMutableArray array];
}

- (void)initializeAppearance {
    [self.navigationBar.right setImage:[UIImage imageNamed:@"搜索"] forState:UIControlStateNormal];
    
    [self.navigationBar addSubview:self.searchBar];
    [self.view addSubview:self.tableView];
}

#pragma mark - URLRequest

- (void)urlRequest {
    [NetRequest getMenuNameRequestWithParameter:@{@"name":self.searchBar.text} succeed:^(id response) {
        if ([response[@"status"] integerValue] == 1) {
            self.modelArr = [NSMutableArray array];
            [self.modelArr addObjectsFromArray:[MenuListModel modelArrForArray:response[@"tngou"]]];
            
            if (self.modelArr.count == 0) {
                [self.view insertSubview:self.notView aboveSubview:self.tableView];
            }else {
                [self.notView removeFromSuperview];
            }
            [self.tableView reloadData];
        }else {
            [Alert alertWithTitle:@"请求出错" message:nil];
        }
    } failure:^(NSError *error) {
        [Alert alertWithTitle:@"请求出错" message:nil];
    }];
}

#pragma mark - respondsTo..

- (void)respondsToRight:(UIButton *)sender {
    if (self.searchBar.text.length != 0) {
        [self urlRequest];
    }
}

- (void)respondsToTap {
    [self.searchBar resignFirstResponder];
}

#pragma mark - UIScrollViewDelegate

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    [self.searchBar resignFirstResponder];
}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.modelArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *reusing = @"MenuSearchCell";
    MenuListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reusing];
    if (!cell) {
        cell = [[MenuListTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reusing];
    }
    MenuListModel *model = self.modelArr[indexPath.row];
    
    NSURL *url = [NSURL URLWithString:model.img];
    [cell.imageViewList sd_setImageWithURL:url placeholderImage:nil options:SDWebImageProgressiveDownload];
    cell.name.text = model.name;
    cell.food.text = model.food;
    cell.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backView.layer.shadowOpacity = 0;
    
    return cell;
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.searchBar resignFirstResponder];
    MenuListModel *model = self.modelArr[indexPath.row];
    DetailViewController *detail = [[DetailViewController alloc] init];
    detail.menuId = model.menuId;
    [self.navigationController pushViewController:detail animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return WXQAdaptionHeight(100);
}

#pragma mark - get

- (UITextField *)searchBar {
    
    if (!_searchBar) {
        
        CGFloat x = CGRectGetMaxX(self.navigationBar.left.frame);
        _searchBar = [[UITextField alloc] initWithFrame:CGRectMake(x, 20, kWIDTH - x - 20, 40)];
        _searchBar.rightView = self.navigationBar.right;
        _searchBar.rightViewMode = UITextFieldViewModeAlways;
        _searchBar.placeholder = @"输入菜谱名称";
        
        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, _searchBar.bounds.size.height - 4, _searchBar.bounds.size.width, 2)];
        line.backgroundColor = [UIColor lightGrayColor];
        [_searchBar addSubview:line];
        
    }
    return _searchBar;
}

- (UITableView *)tableView {
    
    if (!_tableView) {
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, kWIDTH, kHEIGHT - 64) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.tableFooterView = [UIView new];
        
    }
    return _tableView;
}

- (NotView *)notView {
    
    if (!_notView) {
        
        _notView = [[NotView alloc] initWithFrame:self.tableView.frame];
        _notView.backgroundColor = [UIColor whiteColor];
        [_notView.refresh removeFromSuperview];
        _notView.title.text = @"没有搜到您想要的菜谱....";
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(respondsToTap)];
        [_notView addGestureRecognizer:tap];
        
    }
    return _notView;
}


@end














